/**
 * Project Name:cnsy-url-short-service
 * File Name:UserAgentUtil.java
 * Package Name:cn.cnsy123.shorturls.business.util 
 * Date: 2020年4月25日-上午11:58:45
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

/**
 * ClassName: UserAgentUtil
 * 
 * @author yangpengfei
 * @date 2020年4月25日-上午11:58:45
 * @version 1.0
 * @since JDK 1.7
 */
public class UserAgentUtil {
    
    private static final Logger logger = LoggerFactory.getLogger(UserAgentUtil.class);
    
    private static UASparser UA_SPARSER = null;
    
    static {
        try {
            UA_SPARSER = new UASparser(OnlineUpdater.getVendoredInputStream());
        } catch (IOException e) {
            logger.error("发生异常",e);
        }
    }
    
    public static UserAgentInfo getCurrentRequestHeader(String userAgent) {
        try {
            logger.info("解析[user-agent]:{}",userAgent);
            return UA_SPARSER.parse(userAgent);
        } catch (IOException e) {
            logger.error("发生异常",e);
        }
        return null;
    }
    

    public static String getIpAddress(HttpServletRequest request) throws IOException {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
 
        String ip = request.getHeader("X-Forwarded-For");
 
        if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
            if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase("unknown", ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        logger.info("当前请求IP:{}",ip);
        return ip;
    }
    
    
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        if(requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
            return true;
        }
        return false;
    }

}
